<?php
namespace app\services;

 
class BaseServices
{

    protected $rpcServer = 'system';

     /**
     * 数据主体
     * @var array
     */
    protected $data                = [];

    /**
     * 错误码
     * @var int
     */
    protected $code                = 0;

    /**
     * 错误信息
     * @var string
     */
    protected $msg                 = '';

    /**
     * 返回数据体
     * @var array
     */
    protected $returnData         = [];


    protected $system             = [];

    public function __construct(array $system = []){
        $this->system = $system;
    }

    public function getConfig(?string $serverName=null){
        if(empty($serverName)) {
            $serverName = $this->rpcServer;
        }
        $rpc = \think\facade\Config::get('rpc');
        if(empty($rpc[$serverName])) {
            $this->error('没有配置RPCSERVER');
        }

        return $rpc[$serverName];
    }
    
   /**
     * Undocumented function
     * 错误返回
     * @param string $msg
     * @param integer $code
     * @param array $returnData
     * @return void
     */
    public function error(string $msg = null, int $code = 5001, array $returnData = []) {
        throw new \app\CustomException($msg, $code, $returnData);
    }


    public function apiLink(?string $serverName=null,string $action='',array $data=[]) {
        if(strpos($serverName,'http://') !== false || strpos($serverName,'https://') !== false){
            $url = $serverName;
        }else{
            $rpcServer = $this->getConfig($serverName);
            if(empty($rpcServer['url'])) {
                $this->error('未配置URL');
            }
            $url = combinationPath($rpcServer['url'] , $action);
        }
       
        $this->returnData =  $data;

        $sendData = $this->writeJson();
        $header = ['Content-type: application/json;charset=UTF-8'];
        $body = HttpServices::post($url,$sendData,['header'  => $header]);
        // echo '<pre>';
        // var_dump($body,$url);die;
        if(empty($body)) {
            $this->error('请求返回空！');
        }
       
        $post = json_decode($body,true);
        $data = decrypt($post['data'], $post['sig']);
        if (empty($data)) {
            $this->error('解密错误!');
        }
        $data = json_decode($data, true);
        if (empty($data)) {
            $this->error('解密错误,原因：解密出的参数带有特殊符号!');
        }
        // echo '<pre>';
        // dump($url,$data);die;
        $post['data'] = $data;
        return $post;
    }
 
    


    public function writeJson(){
        $this->returnData['ts'] = getMillisecond();
        $this->returnData['systemAppId'] = $this->system['system_appid'];
        $appkey = md5($this->system['system_id'] . '_' . $this->system['system_appid']);
        $sig = getSign($this->returnData,$appkey,$this->system['system_secret']);
        $data = $sig ? encrypt($this->returnData, $sig) : '';
        $data = [
            'code'  => $this->code,
            'message'   => $this->msg,
            'sig' => $sig,
            'data' => $data
        ];
        
        return json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
    }


    /**
     * Undocumented function
     * 验证token
     * @param array $arr
     * @param string $token
     * @return void
     */
    public  function validateToken(array $arr,string $token){
        return (md5((string) $arr['ts']) == $token);
    }

}